<template>
    <div>
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="用户信息">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.keyword"
                        placeholder="账号/昵称/手机号码"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="注册时间">
                    <daterange-picker
                        v-model:startTime="queryParams.create_time_start"
                        v-model:endTime="queryParams.create_time_end"
                    />
                </el-form-item>
                <el-form-item label="注册来源">
                    <el-select class="w-[280px]" v-model="queryParams.channel">
                        <el-option
                            v-for="(item, key) in ClientMap"
                            :key="key"
                            :label="item"
                            :value="key"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="所属城市">
                    <el-select-v2
                        v-model="queryParams.area_id"
                        filterable
                        clearable
                        :options="optionCity"
                        placeholder="请选择"
                        class="w-[280px]"
                        :height="280"
                    />
                </el-form-item>
                <el-form-item label="绑定微信">
                    <el-select class="w-[280px]" v-model="queryParams.wx_open_id" clearable>
                        <el-option label="全部" :value="0" />
                        <el-option label="是" :value="1" />
                        <el-option label="否" :value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="已添加企微">
                    <el-select class="w-[280px]" v-model="queryParams.is_add_qiwei" clearable>
                        <el-option label="全部" :value="0" />
                        <el-option label="是" :value="1" />
                        <el-option label="否" :value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <export-data
                        class="ml-2.5"
                        :fetch-fun="getUserList"
                        :params="queryParams"
                        :page-size="pager.size"
                    />
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-table size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="头像" min-width="100">
                    <template #default="{ row }">
                        <el-avatar :src="row.avatar" :size="50" />
                    </template>
                </el-table-column>
                <el-table-column label="所属城市" min-width="120">
                    <template #default="{ row }">
                        <span v-if="row.reg_area_id" v-city="row.reg_area_id"></span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column label="昵称" prop="nickname" min-width="160" />
                <el-table-column label="账号" prop="account" min-width="140" />
                <el-table-column label="手机号码" prop="mobile" min-width="140" />
                <el-table-column label="注册来源" prop="channel" min-width="120" />
                <el-table-column label="注册时间" prop="create_time" min-width="190" />
                <el-table-column label="是否加V认证" prop="is_vip" min-width="120" >
                    <template #default="{ row }">
                        <el-switch v-model="row.is_vip" v-perms="['user.user/setIsVip']" @change="onChange(row)" :active-value="1" :inactive-value="0" />
                    </template>
                </el-table-column>
                <el-table-column label="绑定微信" prop="wx_open_id" min-width="120">
                    <template #default="{ row }">
                        <span v-if="row.wx_open_id != ''">是</span>
                        <span v-if="row.wx_open_id == ''">否</span>
                    </template>
                </el-table-column>
                <el-table-column label="已添加企微" prop="is_add_qiwei" min-width="120">
                    <template #default="{ row }">
                        <span v-if="row.is_add_qiwei == 1">是</span>
                        <span v-if="row.is_add_qiwei == 2">否</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="140" fixed="right">
                    <template #default="{ row }">
                        <el-button v-perms="['user.user/detail']" type="primary" link>
                            <router-link
                                :to="{
                                    path: getRoutePath('user.user/detail'),
                                    query: {
                                        id: row.id
                                    }
                                }"
                            >
                                详情
                            </router-link>
                        </el-button>
                        <el-button v-if="row.is_forbidden == 0" v-perms="['user.user/detail']" type="danger" link @click="onNo(row)">禁言</el-button>
                        <el-button v-else v-perms="['user.user/detail']" type="warning" link @click="onNo(row)">解禁</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <!-- 禁言时间选择 -->
        <el-dialog
            v-model="dialogVisible"
            title="禁言"
            width="400"
            :close-on-click-modal="false"
        >
            <el-form ref="formRefTime" class="mb-[-16px]" :model="formData" :inline="true" :rules="rules">
                <el-form-item class="inp" label="禁用到日期" prop="times">
                    <el-date-picker
                        class="w-[280px]"
                        v-model="formData.times"
                        type="date"
                        placeholder="请选择日期"
                        value-format="X"
                    />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="onRight">确认</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts" setup name="consumerLists">
import { usePaging } from '@/hooks/usePaging'
import { getRoutePath } from '@/router'
import { getUserList, setIsVip, forbidden } from '@/api/consumer'
import { ClientMap } from '@/enums/appEnums'
import type { FormInstance } from 'element-plus'
const queryParams = reactive({
    keyword: '',
    channel: '',
    create_time_start: '',
    create_time_end: '',
    area_id: '',
    wx_open_id: '',
    is_add_qiwei: ''
})
import useAppStore from '@/stores/modules/app'
const useApp = useAppStore()
const optionCity:any = [{label: '全部',value:''}].concat(useApp.optionCity);

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getUserList,
    params: queryParams
})
onActivated(() => {
    getLists()
})

const onChange = async (row:any) => {
    // console.log(row)
    console.log(row)
    await setIsVip({
        id: row.id,
        is_vip: row.is_vip
    })
    getLists()
}

getLists()
const formData = reactive({
    times: ''
})
const rules = reactive({
    times: [{required: true, message: '请选择日期', trigger: 'blur'}],
})
const id = ref('')
const formRefTime = shallowRef<FormInstance>()
const onNo = (row:any) => {
    id.value = row.id;
    if (row.forbidden_time) {
        formData.times = row.forbidden_time;
    } else {
        formData.times = '';
    }
    formRefTime.value?.clearValidate();
    dialogVisible.value = true;
}
const dialogVisible = ref(false)
const onRight = async () => {
    console.log(formData)
    console.log(id.value)
    // 选择日期 小于当前日期
    // 获取当前的13位时间戳
    var timestamp = new Date().getTime();

    // 将13位时间戳转换为10位数的时间戳
    var tenDigitTimestamp = Math.round(timestamp / 1000).toString();
    console.log(tenDigitTimestamp)

    await forbidden({
        id: id.value,
        times: formData.times > tenDigitTimestamp ? formData.times: 0
    })
    dialogVisible.value = false;
    getLists()
}
</script>
<style lang="scss" scoped>
.inp{
    ::v-deep .el-input__wrapper{
        width: 220px;
    }
}
</style>
